package com.tengju.bff.interfaces.manage.crm.chief.facade;

import com.tengju.bff.interfaces.manage.crm.chief.vo.GetChiefGenSecRelationForm;
import com.tengju.bff.interfaces.shared.ColumnVO;
import com.tengju.bff.interfaces.shared.PageResultVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.user.domain.model.chief.ChiefSecCount;
import com.tengju.user.domain.model.chief.ChiefGenSecRelationRepository;
import com.tengju.user.domain.model.chief.ChiefId;
import com.tengju.user.domain.model.user.SsoUserId;
import com.tengju.user.domain.model.user.StaffInfo;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.service.DepartmentExternalService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: Dylan
 * @date: 2021/7/21 12:22 下午
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ChiefGenSecRelationFacade {

    private final DepartmentExternalService departmentExternalService;
    private final ChiefGenSecRelationRepository chiefGenSecRelationRepository;


    public String getSecIdCodeList(ChiefId chiefId) {
        List<UserIdCode> genSecIdCodeList = chiefGenSecRelationRepository.queryGenSecIdCodeList(chiefId);
        return StringUtils.join(genSecIdCodeList.toArray(), ",");
    }

    public TableVO queryChiefSecRelationByPage(GetChiefGenSecRelationForm getChiefGenSecRelationForm) {

        Integer pageSize = getChiefGenSecRelationForm.getPageSize();
        Integer pageIndex = getChiefGenSecRelationForm.getPageIndex();
        int offset = getChiefGenSecRelationForm.getOffset();

        TableVO table = new TableVO();
        List<ColumnVO> header = List.of(
                ColumnVO.of("realNameAndNickname", "负责人", false),
                ColumnVO.of("department", "部门", false),
                ColumnVO.of("secCount", "已授权SEC个数", false)
        );
        table.setHeader(header);

        int total = chiefGenSecRelationRepository.count(
                Optional.ofNullable(getChiefGenSecRelationForm.getChiefId()).map(ChiefId::new).orElse(null),
                Optional.ofNullable(getChiefGenSecRelationForm.getSecIdCode()).map(UserIdCode::new).orElse(null));


        List<ChiefSecCount> chiefGenSecInfoRelations = new ArrayList<>();


        if (offset <= total) {
            chiefGenSecInfoRelations = chiefGenSecRelationRepository.queryByChiefIdAndSecIdPage(
                    Optional.ofNullable(getChiefGenSecRelationForm.getChiefId()).map(ChiefId::new).orElse(null),
                    Optional.ofNullable(getChiefGenSecRelationForm.getSecIdCode()).map(UserIdCode::new).orElse(null),
                    getChiefGenSecRelationForm.getOffset(),
                    getChiefGenSecRelationForm.getPageSize());
        }

        List<StaffInfo> staffInfoList = departmentExternalService.getStaffInfos(chiefGenSecInfoRelations.stream().map(ChiefSecCount::getChiefId).filter(Objects::nonNull)
                .map(v -> new SsoUserId(v.getValue())).collect(Collectors.toList()));

        Map<ChiefId, StaffInfo> map = staffInfoList.stream().collect(Collectors.toMap(v1 -> new ChiefId(v1.getSsoUserId().getValue()), v2 -> v2, (v1, v2) -> v1));

        List<Map<String, Object>> records = new ArrayList<>();

        for (ChiefSecCount chiefGenSecInfoRelation : chiefGenSecInfoRelations) {
            Map<String, Object> row = new HashMap<>();
            row.put("realNameAndNickname", getChiefName(map, chiefGenSecInfoRelation) + "(" + getNickName(map, chiefGenSecInfoRelation) + ")");
            row.put("department", getDepartmentFullName(map, chiefGenSecInfoRelation));
            row.put("chiefId", chiefGenSecInfoRelation.getChiefId().getValue());
            row.put("secCount", chiefGenSecInfoRelation.getSecCount() + "个");
            records.add(row);
        }
        table.setRecords(records);
        table.setPage(PageResultVO.of(pageIndex, pageSize, total));
        return table;
    }

    public String getChiefName(Map<ChiefId, StaffInfo> map, ChiefSecCount chiefGenSecInfoRelation) {
        StaffInfo staffInfo = map.get(chiefGenSecInfoRelation.getChiefId());
        return Optional.ofNullable(staffInfo).map(StaffInfo::getRealName).orElse("");
    }

    public String getNickName(Map<ChiefId, StaffInfo> map, ChiefSecCount chiefGenSecInfoRelation) {
        StaffInfo staffInfo = map.get(chiefGenSecInfoRelation.getChiefId());
        return Optional.ofNullable(staffInfo).map(StaffInfo::getNickname).orElse("");
    }

    public String getDepartmentFullName(Map<ChiefId, StaffInfo> map, ChiefSecCount chiefGenSecInfoRelation) {
        StaffInfo staffInfo = map.get(chiefGenSecInfoRelation.getChiefId());
        return Optional.ofNullable(staffInfo).map(StaffInfo::getDepartmentFullName).orElse("");
    }

}
